package org.cjlm.ui.pub;

import org.cjlm.ui.BoxMain;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.Map;

public abstract class MAbstraction extends AbstractAction {

    private BoxMain boxMain;

    private JDesktopPane desktop;

    private Map<String, MInternalFrame> frameRegistry;

    public MAbstraction(String title){
        super(title);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try{
            doAction(e);
        }catch (Exception ex){
            showErrorDialog("错误", "操作失败", ex);
        }
    }

    public abstract void doAction(ActionEvent e) throws Exception;

    public void setDesktop(JDesktopPane desktop) {
        this.desktop = desktop;
    }

    public JDesktopPane getDesktop() {
        return desktop;
    }

    public Map<String, MInternalFrame> getFrameRegistry() {
        return frameRegistry;
    }

    public void setFrameRegistry(Map<String, MInternalFrame> frameRegistry) {
        this.frameRegistry = frameRegistry;
    }

    public BoxMain getBoxMain() {
        return boxMain;
    }

    public void setBoxMain(BoxMain boxMain) {
        this.boxMain = boxMain;
    }

    public void showErrorDialog(String title, String message, Exception e){
        JOptionPane.showMessageDialog(getBoxMain(), message+"："+e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
        throw new RuntimeException(e);
    }

    public void showErrorDialog(String title, String message){
        JOptionPane.showMessageDialog(getBoxMain(), message, title, JOptionPane.ERROR_MESSAGE);
    }

    public void showHintDialog(String title, String message){
        JOptionPane.showMessageDialog(getBoxMain(), message, title, JOptionPane.INFORMATION_MESSAGE);
    }

    public int showYesNoDialog(String title, String message){
        return JOptionPane.showConfirmDialog(getBoxMain(), message, title, JOptionPane.YES_NO_OPTION);
    }
}
